package demo3;
class Animal {
    public String name;
    public int age;
    public Animal(String name,int age) {
        this.name = name;
        this.age = age;
    }
    public void eat() {
        System.out.println(this.name + "正在吃....");
    }
}
class Dog extends Animal{
    public String color;
    public Dog(String name,int age,String color) {
        super(name,age);
        this.color = color;
    }
    public void eat() {
        System.out.println(this.name + "正在吃狗粮....");
    }
    public void barks() {
        System.out.println(this.name + "正在汪汪汪叫....");
    }
}
public class Test {
    public static void main(String[] args) {
        //多态：完成某一行为时，不同对象执行产生结果不同
        Dog dog = new Dog("小黑",2,"黑色");
        dog.eat();
        dog.barks();
        //向上转型：父类引用调用子类引用
        Animal animal  = new Dog("咪咪",2,"奶白色");
        animal.eat();
        //实现多态的条件：
        //1.必须是在继承条件下
        //2.父类引用调用重写的子类方法
        //3.子类必须对父类方法重写
    }
}
